<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu class="el-menu-top" mode="horizontal" background-color="#23262E" text-color="#fff" active-text-color="#409EFF">
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img :src="userInfo.user_pic" alt="" class="avatar" v-if="userInfo.user_pic" />
            <img src="../../assets/logo.png" alt="" class="avatar" v-else />
            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1" @click="$router.push('/user-info')"><i class="el-icon-s-operation"></i>基本资料 </el-menu-item>
          <el-menu-item index="1-2" @click="$router.push('/user-avatar')"><i class="el-icon-camera"></i>更换头像 </el-menu-item>
          <el-menu-item index="1-3" @click="$router.push('/user-pwd')"><i class="el-icon-key"></i>重置密码 </el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="logout"><i class="el-icon-switch-button"></i>退出 </el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <!-- 左侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <img :src="userInfo.user_pic" alt="" v-if="userInfo.user_pic" />
          <img src="../../assets/logo.png" alt="" v-else />
          <span>欢迎 {{ userInfo.nickname || userInfo.username }}</span>
        </div>
        <!-- 菜单区域 -->
        <el-menu :default-active="$route.path" class="el-menu-vertical-demo" background-color="#23262E" text-color="#fff" active-text-color="#409EFF" unique-opened router>
          <!-- template：模板标签，起到包裹作用 不会渲染到dom结构 -->
          <!-- tempalte：不能使用key -->
          <template v-for="item in menusList">
            <!-- 不包含子菜单的“一级菜单” -->
            <el-menu-item :index="item.indexPath" :key="item.indexPath" v-if="!item.children"> <i :class="item.icon"></i>{{ item.title }} </el-menu-item>
            <!-- 包含子菜单的“一级菜单” -->
            <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item :index="childItem.indexPath" v-for="childItem in item.children" :key="childItem.indexPath"><i :class="childItem.icon"></i>{{ childItem.title }} </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <!-- 子路由出口 -->
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'Main',
  data() {
    return {
      menusList: []
    }
  },
  computed: {
    // 将state中的userInfo映射为当前组件的计算属性
    ...mapState('user', ['userInfo', 'token'])
  },
  created() {
    // 1.调用ations中的函数
    // this.$store.dispatch('actions中的函数名')
    this.$store.dispatch('user/initUserInfo'),
    // 调用初始化左侧菜单的函数
    this.initMenus()
  },
  methods: {
    // 1.退出登录，绑定事件
    logout() {
      // 2.弹出询问框
      this.$confirm('您确认退出吗?', '呜呜呜', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // todo：执行退出的操作
          this.$message.success('退出成功,欢迎再次登录')
          // 3.清空token
          this.$store.commit('user/updateToken', '')
          // 清空userInof
          this.$store.commit('user/updateUserInfo', {})
          // 4.跳转到登录页
          this.$router.push('/login')
        })
        .catch(err => err)
    },
    // 初始化左侧菜单的数据
    async initMenus() {
      const { data: res } = await this.$http.get('/my/menus', {
        headers: {
          Authorization: this.$store.state.user.token
        }
      })
      console.log(res)
      if (res.code === 0) {
        this.menusList = res.data
      }
    }
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  // 左侧边栏用户信息区域
  .user-box {
    height: 70px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-top: 1px solid #000;
    border-bottom: 1px solid #000;
    user-select: none;
    img {
      width: 35px;
      height: 35px;
      border-radius: 50%;
      background-color: #fff;
      margin-right: 15px;
      object-fit: cover;
    }
    span {
      color: white;
      font-size: 12px;
    }
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
